package com.centaline.androidsalesblog.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.baidu.mapapi.model.LatLng;
import com.bumptech.glide.DrawableRequestBuilder;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.adapter.viewholder.EsfEstateVH;
import com.centaline.androidsalesblog.model.databaseobject.EsfEstateDo;
import com.centaline.androidsalesblog.utils.CentaDistanceUtil;
import com.centaline.androidsalesblog.utils.PriceUtil;
import com.centanet.framework.base.BaseAdapter1;
import com.centanet.framework.glide.GlideLoad;

import java.util.ArrayList;

/**
 * 小区列表
 * Created by guilin on 16/1/11.
 */
public class EsfEstateAdapter extends BaseAdapter1<EsfEstateVH> {

    private final DrawableRequestBuilder<String> requestBuilder;
    private final ArrayList<EsfEstateDo> esfEstateDos;
    private LatLng locLatLng;

    public EsfEstateAdapter(Context context, DrawableRequestBuilder<String> requestBuilder,
                            ArrayList<EsfEstateDo> esfEstateDos) {
        super(context);
        this.requestBuilder = requestBuilder;
        this.esfEstateDos = esfEstateDos;
    }

    public void setLocation(LatLng locLatLng) {
        this.locLatLng = locLatLng;
    }

    @Override
    protected int inflateByLayoutId() {
        return R.layout.item_esf_estate;
    }

    @Override
    protected EsfEstateVH viewHolder(View view) {
        return new EsfEstateVH(view);
    }

    @Override
    public void onBindViewHolder(EsfEstateVH holder, int position) {
        final EsfEstateDo esfEstateDo = esfEstateDos.get(position);
        GlideLoad.load(new GlideLoad.Builder(requestBuilder, esfEstateDo.getDefaultImg())
                .placeHolder(R.drawable.ic_default_est_estate)
                .error(R.drawable.ic_default_est_estate)
                .openWifiSwitch()
                .into(holder.img_logo));
        holder.img_360.setVisibility(TextUtils.isEmpty(esfEstateDo.getTencentVistaUrl()) ? View.GONE : View.VISIBLE);
        holder.atv_price.setText(PriceUtil.esfSaleAvgPrice(esfEstateDo.getCestAvgPrice()));
        holder.atv_name.setText(esfEstateDo.getName());
        StringBuilder regionGscop = new StringBuilder();
        if (!TextUtils.isEmpty(esfEstateDo.getRegion())) {
            regionGscop.append(esfEstateDo.getRegion());
        }
        if (!TextUtils.isEmpty(esfEstateDo.getScope())) {
            if (!TextUtils.isEmpty(esfEstateDo.getRegion())) {
                regionGscop.append("-");
            }
            regionGscop.append(esfEstateDo.getScope());
        }
        holder.atv_desc.setText(regionGscop.toString());
        int saleCount = esfEstateDo.getSaleCount();
        int rentCount = esfEstateDo.getRentCount();
        StringBuilder saleAndRent = new StringBuilder();
        if (saleCount > 0) {
            saleAndRent.append("在售");
            saleAndRent.append(saleCount);
            saleAndRent.append("套");
            if (rentCount > 0) {
                saleAndRent.append(" | ");
                saleAndRent.append("在租");
                saleAndRent.append(rentCount);
                saleAndRent.append("套");
            }
        } else {
            saleAndRent.append("在租");
            saleAndRent.append(rentCount);
            saleAndRent.append("套");
        }
        holder.atv_sale_rent.setText(saleAndRent.toString());

        CentaDistanceUtil.distanceView(holder.atv_distance, locLatLng,
                new LatLng(esfEstateDo.getLat(), esfEstateDo.getLng()));
    }

    @Override
    public int getItemCount() {
        return esfEstateDos.size();
    }
}
